//
//  BaseViewController.m
//  funLove
//
//  Created by MZ on 2022/3/11.
//

#import "BaseViewController.h"
#import "AppDelegate.h"
static const void *KClickBlock = @"clickBlock";
static const void *KEmptyText = @"emptyText";
static const void *KOffSet = @"offset";
static const void *Kimage = @"emptyImage";

@interface BaseViewController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@end

@implementation BaseViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self SQ_viewWillAppear:animated];
}
-(void)SQ_viewWillAppear:(BOOL)animated{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.navigationController.navigationBar.hidden = YES;
  
    if (@available(iOS 13.0, *)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.navigationController.navigationBar.translucent = NO;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

    self.view.backgroundColor = UIColor.blackColor;
    self.page = 1;
    self.offset = -100;
    self.showEmpty = YES;
    self.zx_backBtnImageName= @"返回";
    self.zx_navStatusBarStyle = ZXNavStatusBarStyleDefault;
    self.zx_navLeftBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.zx_navTitleFont = FONT_BOLD(18);
    self.zx_navTitleColor = SQColor(@"#FFFFFF");
    self.zx_navLineView.hidden = YES;
    self.zx_navBarBackgroundColor = UIColor.clearColor;
    self.zx_navBar.zx_leftBtn.zx_fixWidth = 40;
    self.zx_navBar.zx_leftBtn.zx_fixHeight = 40;
    self.zx_navBar.zx_leftBtn.zx_fixImageSize = CGSizeMake(20, 20);
    self.zx_navBar.zx_leftBtn.zx_fixMarginLeft = 10;
    
    self.zx_navBar.zx_rightBtn.zx_fixWidth = 40;
    self.zx_navBar.zx_rightBtn.zx_fixHeight = 60;
    self.zx_navBar.zx_rightBtn.zx_fixImageSize = CGSizeMake(20, 20);
    self.zx_navBar.zx_rightBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.zx_navBar.zx_rightBtn.zx_fixMarginLeft = 0;
    self.zx_navRightBtn.mm_acceptEventInterval = 1;
    self.zx_navRightBtn.tintColor = UIColor.whiteColor;

    
//    self.zx_navSubRightBtn.zx_fixMarginRight = 10;
    self.zx_navBar.zx_subRightBtn.zx_fixWidth = 40;
    self.zx_navBar.zx_subRightBtn.zx_fixHeight = 40;
    self.zx_navBar.zx_subRightBtn.zx_fixImageSize = CGSizeMake(20, 20);
    self.zx_navBar.zx_subRightBtn.zx_fixMarginLeft = 0;
    self.zx_navRightBtn.mm_acceptEventInterval = 1;
    self.zx_navLeftBtn.layer.cornerRadius = 10;
 
    
    [self SQ_UIViewForSelfView];
 
    
}

/// 添加ui
-(void)SQ_UIViewForSelfView
{
    
}

-(void)SQ_Header{
    
}
-(void)SQ_Foother{
    
}
-(void)dealloc{
    
}
-(void)SQ_tableViewAddRefresh{
    self.tableView.mj_header = self.header;
    self.tableView.mj_footer = self.footer;
}
-(void)SQ_collectViewAddRefresh{
    self.collectionView.mj_header = self.header;
    self.collectionView.mj_footer = self.footer;
}

- (MJRefreshNormalHeader *)header{
    if (!_header) {
        _header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(SQ_Header)];
        _header.lastUpdatedTimeLabel.hidden = YES;
        _header.stateLabel.hidden = YES;
    }
    return _header;
}
- (MJRefreshAutoNormalFooter *)footer{
    if (!_footer) {
        _footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(SQ_Foother)];
        _footer.refreshingTitleHidden = NO;
        _footer.stateLabel.hidden = YES;
    }
    return _footer;
}



-(void)SQ_UICollectionViewRegisterClass:(NSString*)string
{
    [_collectionView registerClass:[NSClassFromString(string) class] forCellWithReuseIdentifier:string];
}


- (void)SQ_presentFull:(UIViewController *)vc {
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)SQ_pushViewControllerFull:(UIViewController *)vc
{
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//导航返回
-(void)SQ_popViewVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)SQ_clearnNavitem{
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
      //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.backgroundColor = UIColor.clearColor;
}
 
- (void)setShowEmpty:(BOOL)showEmpty{
    _showEmpty = showEmpty;
}
///空白页
- (ClickBlock)clickBlock{
    return objc_getAssociatedObject(self, &KClickBlock);
}


- (void)setClickBlock:(ClickBlock)clickBlock{
 
    objc_setAssociatedObject(self, &KClickBlock, clickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)emptyText{
    return objc_getAssociatedObject(self, &KEmptyText);
}

- (void)setEmptyText:(NSString *)emptyText{
    objc_setAssociatedObject(self, &KEmptyText, emptyText, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGFloat)offset{
    
    NSNumber *number = objc_getAssociatedObject(self, &KOffSet);
    return number.floatValue;
}
- (void)setOffset:(CGFloat)offset{
    
    NSNumber *number = [NSNumber numberWithDouble:offset];
    
    objc_setAssociatedObject(self, &KOffSet, number, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)emptyImage{
    return objc_getAssociatedObject(self, &Kimage);
}

- (void)setEmptyImage:(NSString *)emptyImage{
    objc_setAssociatedObject(self, &Kimage, emptyImage, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

#pragma mark - DZNEmptyDataSetSource

// 空白界面的标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = self.emptyText?:@"";
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    paragraph.lineSpacing = 5;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                 NSParagraphStyleAttributeName: paragraph};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

// 空白页的图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return SQGetImage(self.emptyImage)?:[UIImage imageNamed:@"无好友空置"];
}

//是否允许滚动，默认NO
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

// 垂直偏移量
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    return self.offset;
}
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return 20;
}
#pragma mark - DZNEmptyDataSetDelegate 点击事件
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    if (self.clickBlock) {
        self.clickBlock();
    }
}

-(void)closeKeyBoard{
    [self.view endEditing:YES];
}



-(UITableView*)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 0) style:UITableViewStylePlain];
//        _tableView.dataSource = self;
//        _tableView.delegate = self;
        if (_showEmpty) {
            _tableView.emptyDataSetDelegate = self;
            _tableView.emptyDataSetSource = self;
        }
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0.01; // 这个属性，设置为0 时，headerView会随着滑动，给个最小值，就不会滑动了。
         }
        _tableView.allowsMultipleSelection = NO;
        if (@available(iOS 13.0, *)) {
            _tableView.automaticallyAdjustsScrollIndicatorInsets = NO;
        } else {
            // Fallback on earlier versions
        }
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 11.0, *)) {
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
            _tableView.estimatedRowHeight = 0;
        }
        
        
    }
    return _tableView;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.userInteractionEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.alwaysBounceVertical = YES;
//        _collectionView.delegate = self;
//        _collectionView.dataSource = self;
        if (_showEmpty) {
            _collectionView.emptyDataSetDelegate = self;
            _collectionView.emptyDataSetSource = self;
        }
    }
    return _collectionView;
}

-(NSMutableArray*)jsonArray
{
    if (!_jsonArray) {
        _jsonArray = [[NSMutableArray alloc]init];
    }
    return _jsonArray;
}
 
 


@end
